﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAO_DTO_WS.DTO;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace DAO_DTO_WS.DAO
{
    public class DoanhNghiepDAO : Abstract
    {
        public static bool ThemMoi(DoanhNghiepDTO doanhNghiep)
        {
            SqlConnection connection = KetNoi();
            bool KetQua = false;
            String SqlString = "INSERT INTO DOANHNGHIEP(TenDoanhNghiep,DiaChi,DienThoai,WebSite,TenDangNhap,MatKhau,TinhTrang) VALUES " +
                 "(@TenDoanhNghiep,@DiaChi,@DienThoai,@WebSite,@TenDangNhap,@MatKhau,@TinhTrang)";
            try
            {
                SqlCommand command = new SqlCommand(SqlString, connection);
                command.Parameters.AddWithValue("@TenDoanhNghiep", doanhNghiep.TenDoanhNghiep);
                command.Parameters.AddWithValue("@DiaChi", doanhNghiep.DiaChi);
                command.Parameters.AddWithValue("@DienThoai", doanhNghiep.DienThoai);
                command.Parameters.AddWithValue("@WebSite", doanhNghiep.WebSite);
                command.Parameters.AddWithValue("@TenDangNhap", doanhNghiep.TenDangNhap);
                command.Parameters.AddWithValue("@MatKhau", doanhNghiep.MatKhau);
                command.Parameters.AddWithValue("@TinhTrang", doanhNghiep.TinhTrang);

                int rows = 0;
                rows = command.ExecuteNonQuery();
                if (rows == 1)
                    KetQua = true;
            }
            catch (Exception ex)
            {
                throw ex;
                KetQua = false;
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return KetQua;
        }
        public static DoanhNghiepDTO LayDNTheoMa(int maDoanhNghiep)
        {
            DoanhNghiepDTO DoanhNghiep = new DoanhNghiepDTO();
            SqlConnection connection = KetNoi();
            string sqlstring = "SELECT * from DOANHNGHIEP where MaDoanhNghiep =@MaDN";
            try
            {
                SqlCommand command = new SqlCommand(sqlstring, connection);
                command.Parameters.AddWithValue("@MaDN", maDoanhNghiep);
                SqlDataReader reader = command.ExecuteReader();
                reader.Read();
                DocDuLieu(reader, DoanhNghiep);
            }
            catch
            {
                DoanhNghiep = new DoanhNghiepDTO();
                DoanhNghiep.TinhTrang = -1;
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return DoanhNghiep;
        }
        public static DoanhNghiepDTO LayDNTheoTenDangNhap(string tendangnhap)
        {
            DoanhNghiepDTO DoanhNghiep = new DoanhNghiepDTO();
            SqlConnection connection = KetNoi();
            string sqlstring = "SELECT * from DOANHNGHIEP where TenDangNhap =@MaDN";
            try
            {
                SqlCommand command = new SqlCommand(sqlstring, connection);
                command.Parameters.AddWithValue("@MaDN", tendangnhap);
                SqlDataReader reader = command.ExecuteReader();
                reader.Read();
                DocDuLieu(reader, DoanhNghiep);
            }
            catch
            {
                DoanhNghiep = new DoanhNghiepDTO();
                DoanhNghiep.TinhTrang = -1;
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return DoanhNghiep;
        }
        public static bool Xoa(string tendangnhap)
        {
            SqlConnection connection = KetNoi();
            bool KetQua = false;
            String SqlString = "DELETE FROM DOANHNGHIEP WHERE TenDangNhap=@tendangnhap";
            try
            {
                SqlCommand command = new SqlCommand(SqlString, connection);
                command.Parameters.AddWithValue("@tendangnhap", tendangnhap);
                int rows = 0;
                rows = command.ExecuteNonQuery();
                if (rows == 1)
                    KetQua = true;
            }
            catch (Exception ex)
            {
                throw ex;
                KetQua = false;
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return KetQua;
        }
      
        public static bool SuaDN(DoanhNghiepDTO doanhnghiep)
        {
            SqlConnection connection = KetNoi();
            bool KetQua = false;
            String SqlString = "UPDATE DOANHNGHIEP " +
                "SET TenDoanhNghiep=@TenDoanhNghiep,DiaChi=@DiaChi,DienThoai=@DienThoai,WebSite=@WebSite,TenDangNhap=@TenDangNhap,MatKhau=@MatKhau,TinhTrang=@TinhTrang " +
                "WHERE MaDoanhNghiep=@MaDoanhNghiep";

            try
            {
                SqlCommand command = new SqlCommand(SqlString, connection);
                command.Parameters.AddWithValue("@TenDoanhNghiep", doanhnghiep.TenDoanhNghiep);
                command.Parameters.AddWithValue("@DiaChi", doanhnghiep.DiaChi);
                command.Parameters.AddWithValue("@DienThoai", doanhnghiep.DienThoai);
                command.Parameters.AddWithValue("@WebSite", doanhnghiep.WebSite);
                command.Parameters.AddWithValue("@TenDangNhap", doanhnghiep.TenDangNhap);
                command.Parameters.AddWithValue("@MatKhau", doanhnghiep.MatKhau);
                command.Parameters.AddWithValue("@TinhTrang", doanhnghiep.TinhTrang);
                command.Parameters.AddWithValue("@MaDoanhNghiep", doanhnghiep.MaDoanhNghiep);

                int rows = 0;
                rows = command.ExecuteNonQuery();
                if (rows == 1)
                    KetQua = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return KetQua;
        }
        public static bool KiemTra(string tendangnhap)
        {
            SqlConnection connection = KetNoi();
            DoanhNghiepDTO tv = new DoanhNghiepDTO();
            bool kt = false;
            String SqlString = "select * from DOANHNGHIEP where TenDangNhap=@tendangnhap";
            try
            {
                SqlCommand lenh = new SqlCommand(SqlString, connection);
                lenh.Parameters.AddWithValue("@tendangnhap", tendangnhap);
                SqlDataReader Doc = lenh.ExecuteReader();
                kt = Doc.Read();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return kt;
        }

        public static List<DoanhNghiepDTO> LayDSDoanhNghiep()
        {
            SqlConnection connection = KetNoi();
            List<DoanhNghiepDTO> dsdn = new List<DoanhNghiepDTO>();
            String SqlString = "select * from DOANHNGHIEP";
            try
            {
                SqlCommand lenh = new SqlCommand(SqlString, connection);
                SqlDataReader Doc = lenh.ExecuteReader();
                while (Doc.Read())
                {
                    DoanhNghiepDTO doanhnghiep = new DoanhNghiepDTO();
                    DocDuLieu(Doc, doanhnghiep);

                    dsdn.Add(doanhnghiep);
                }
            }
            catch (Exception ex)
            {
                throw ex;
                dsdn = new List<DoanhNghiepDTO>();
            }
            finally
            {
                if ((connection != null) && (connection.State == ConnectionState.Open))
                    connection.Close();
            }
            return dsdn;
        }

        private static void DocDuLieu(SqlDataReader doc, DoanhNghiepDTO doanhnghiep)
        {
            doanhnghiep.MaDoanhNghiep = doc.GetInt32(0);
            doanhnghiep.TenDoanhNghiep = doc.GetString(1);
            doanhnghiep.DiaChi = doc.GetString(2);
            doanhnghiep.DienThoai = doc.GetString(3);
            doanhnghiep.WebSite = doc.GetString(4);
            doanhnghiep.TenDangNhap = doc.GetString(5);
            doanhnghiep.MatKhau = doc.GetString(6);
            doanhnghiep.TinhTrang = doc.GetInt32(7);
        }
    }
}
